import common.CommonFunction;
import common.Mask;

import flash.events.MouseEvent;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.events.ListEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

[Embed(source="/images/success.png")]private var successIcon:Class;
[Embed(source="/images/error.png")]private var errorIcon:Class;
[Embed(source="/images/question.png")]private var questionIcon:Class;

public var infoObject:Object;

public function onCreationComplete():void
{
	infoObject = new Object();
	infoObject["date"] = new Date();	
	getRoles.send(infoObject);
	Mask.show(CommonFunction.getMessages('pleaseWait'));
}

public var customerXML:XML;
public var customerList:ArrayCollection;
public function onSuccess(event:ResultEvent):void
{
	Mask.close();
	customerXML = event.result as XML;
	
	customerList = new ArrayCollection();
	for each(var obj:Object in customerXML.record)
	{
		customerList.addItem({select:true, code:obj.code, value:obj.description, grpCode:obj.grpCode});
	}
	role.dataProvider = customerList;
	role.labelField = "value";
}

public function onFailure(event:FaultEvent):void
{
	Mask.close();
	Alert.show(event.message + "");
}

protected function role_changeHandler(event:ListEvent):void
{
		infoObject = new Object();
		
		infoObject["date"] = new Date();
		infoObject["code"] = role.selectedItem.code;
		
		getDesc.send(infoObject);
}

public function getSuccess(event:ResultEvent):void
{
	customerXML = event.result as XML;
	
	customerList = new ArrayCollection();
	if(customerXML.record.length() > 0)
	{
		for each(var obj:Object in customerXML.record)
		{
			customerList.addItem({select:true, code1:obj.code, col2:obj.description});
		}
		customerDetailsGrid.dataProvider = customerList;
		second.visible = true;
	}
	else
	{
		Alert.show("All the Privilages Have Been Assigned to the Selected Role.", "Information");
		second.visible = false;
	}
}

protected function approve_clickHandler(event:MouseEvent):void
{
	customerList = new ArrayCollection();
	for each(var obj:Object in customerDetailsGrid.dataProvider)
	{
		if(obj.select == true)
		{
			customerList.addItemAt(obj.code1, 0);
		}
	}
	if(customerList.length == 0)
	{
		Alert.show(CommonFunction.getMessages('selectARecord'),
			CommonFunction.getMessages('error'), 0, null, null);	
	}
	else
	{	
		Alert.show(CommonFunction.getMessages('areyousure'),
			CommonFunction.getMessages('confirm'), (Alert.YES|Alert.NO), null, onValidation);		
	}
}

protected function onValidation(event:CloseEvent):void
{
	if(event.detail == Alert.YES)
	{
		infoObject = new Object();
		infoObject["date"] = new Date();
		infoObject["selectdCustomers"] = customerList;
		infoObject["roleCode"] = role.selectedItem.code;
		Mask.show(CommonFunction.getMessages('pleaseWait'));
		approveRole.send(infoObject);
	}
}

public function approveSuccess(event:ResultEvent):void
{
	Mask.close();
	customerXML = event.result as XML;
	if(customerXML.booleanvalue == "true")
	{
		Alert.show(CommonFunction.getMessages('reqSuccess'), "Success");
		infoObject = new Object();
		
		infoObject["date"] = new Date();
		infoObject["code"] = role.selectedItem.code;
		
		getDesc.send(infoObject);
	}
	else
	{
		Alert.show(CommonFunction.getMessages('reqFailure'), "Failure");
	}
}